// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System.Collections;
using Microsoft.WindowsMobile.Status;
using svs.Mobile.BeyondMedia.FeedCore;

namespace svs.Mobile.BeyondMedia.Objects
{
	internal class NotificationManager
	{
		private ArrayList m_Notifications = new ArrayList();
		private bool m_CallActivelyPaused = false;

		private void PhoneStateChanged(object sender, ChangeEventArgs args)
		{
			SystemState state = (SystemState) sender;

			string ev = "Event: " + state.Property.ToString() + ", Value:";

			if (args.NewValue == null)
				ev += "Null";
			else
				ev += args.NewValue.ToString();
			
			CoreHelper.WriteTraceEntry(ev);

			//Detect start of a call
			if (args.NewValue != null && args.NewValue is int && (int) args.NewValue > 0
			    && (state.Property == SystemProperty.PhoneIncomingCall || state.Property == SystemProperty.PhoneActiveCallCount))
				if (ViewManager.MediaPlayer.IsPlaying)
				{
					CoreHelper.WriteTraceEntry("Incoming call, Found Plying! Paused.");
					CommandManager.CmdPause();
					m_CallActivelyPaused = true;
				}
				else
					m_CallActivelyPaused = false;

			//Detect end of a call
			if (state.Property == SystemProperty.PhoneActiveCallCount &&
			    (args.NewValue == null || (args.NewValue is int && (int) args.NewValue == 0)))
			{
				if (m_CallActivelyPaused)
				{
					CommandManager.CmdPlay();
					CoreHelper.WriteTraceEntry("Finished! Resuming play...");
					m_CallActivelyPaused = false;
				}
			}
		}

		public void StopMonitoring()
		{
			m_Notifications.Clear();
		}

		public void StartMonitoring()
		{
			if (IsPhoneDevice)
			{
				m_Notifications.Clear();

				m_Notifications.Add(new SystemState(SystemProperty.PhoneActiveCallCount));
				m_Notifications.Add(new SystemState(SystemProperty.PhoneIncomingCall));

				foreach (SystemState s in m_Notifications)
				{
					s.Changed += new ChangeEventHandler(PhoneStateChanged);
				}
			}
		}

		public bool IsPhoneDevice
		{
			get { return SystemState.PhoneRadioPresent; }
		}
	}
}